from nltk.corpus import wordnet as wn
import sys

from nltk.corpus import wordnet_ic
brown_ic = wordnet_ic.ic('ic-brown.dat')


class WnSimilarity: 
	kw = None
	kw_raw = None
	def setKeyword(self, keyword):
		self.kw_raw = keyword
		self.kw = wn.synsets(keyword)

	def calcLch(self, word, similarity_method):
		if(word == self.kw_raw):
			return 1
		try:
			synsets = wn.synsets(word)
		except:
			synsets = []
		similarity = 0
		for w in self.kw:
			for s in synsets:
				try:
					if similarity_method=="lch":
						sim = w.lch_similarity(s,brown_ic)
					if similarity_method=="jcn":
						sim = w.jcn_similarity(s,brown_ic)
					if similarity_method=="wup":
						sim = w.wup_similarity(s,brown_ic)
					if similarity_method=="lin":
						sim = w.lin_similarity(s,brown_ic)
					if sim == None:
						sim = 0

				except Exception as e:
					sim = 0

				if sim > 10:
					sim = 0

				if sim > similarity:
					similarity = sim
#				print "WORDS: %s->%s=%f"%(w.name, s.name, sim)
		return similarity
